package com.ooono.factorytest.items;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.util.Log;
import android.widget.TextView;

import com.ooono.factorytest.R;
import com.ooono.factorytest.base.BaseActivity;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import mcs8.mdm.PolicyManager;

public class InfoTestActivity extends BaseActivity {

    private static final String TAG = "InfoTestActivity";
    public static final String KEY = "info";

    @Override
    public int getContentLayout() {
        return R.layout.content_info_test;
    }

    @Override
    public void initViews() {
        String[] deviceInfo = PolicyManager.getDeviceInfo();

        TextView modelTv = findViewById(R.id.tv_model);
        modelTv.setText(Build.MODEL);
        TextView androidVersionTv = findViewById(R.id.tv_android_version);
        androidVersionTv.setText(Build.VERSION.RELEASE);
        TextView firmwareVersionTv = findViewById(R.id.tv_firmware_version);
        firmwareVersionTv.setText(Build.DISPLAY);
        TextView wifiMacTv = findViewById(R.id.tv_wifi_mac);
        wifiMacTv.setText(getWifiMac());
        TextView btMacTv = findViewById(R.id.tv_bt_mac);
        btMacTv.setText(getBtMac());
        TextView ethMacTv = findViewById(R.id.tv_eth_mac);
        ethMacTv.setText(getEth0Mac());
        TextView snTv = findViewById(R.id.tv_sn);
        snTv.setText(PolicyManager.getDeviceSn());
        TextView imeiTv = findViewById(R.id.tv_imei);
        imeiTv.setText(PolicyManager.getDeviceImei());
        TextView romSizeTv = findViewById(R.id.tv_rom_size);
        romSizeTv.setText(deviceInfo[3]);
        TextView ramSizeTv = findViewById(R.id.tv_ram_size);
        ramSizeTv.setText(deviceInfo[2]);
        TextView resolutionTv = findViewById(R.id.tv_resolution);
        resolutionTv.setText(deviceInfo[4]);
        TextView locationTv = findViewById(R.id.tv_location);
        String beidouChip = PolicyManager.getSystemProperties("ro.beidou.chip", "0");
        if ("1".equals(beidouChip)) {
            locationTv.setText("北斗芯片");
        }

    }

    @Override
    public int getTestTitle() {
        return R.string.info_test_title;
    }

    @Override
    public String getTestKey() {
        return KEY;
    }


    @SuppressLint("HardwareIds")
    private String getWifiMac() {
        WifiManager wifiManager = (WifiManager) getApplicationContext()
                .getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        return wifiInfo.getMacAddress();
    }


    @SuppressLint("HardwareIds")
    private String getBtMac() {
        // 需要权限，在应用层无法使用
        BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
        return ba.getAddress();
    }

    private String getEth0Mac() {
        String sEth0Mac = "";
        try {
            int numRead = 0;
            char[] buf = new char[1024];
            StringBuffer strBuf = new StringBuffer(1000);
            BufferedReader reader = new BufferedReader(new FileReader(
                    "/sys/class/net/eth0/address"));
            while ((numRead = reader.read(buf)) != -1) {
                String readData = String.valueOf(buf, 0, numRead);
                strBuf.append(readData);
            }
            sEth0Mac = strBuf.toString().replaceAll("\r|\n", "");
            reader.close();
        } catch (IOException ex) {
            Log.w(TAG, "eth0 mac not exist");
        }
        return sEth0Mac;
    }
}
